import * as types from './actionTypes';
import Immutable from 'seamless-immutable';
const initialState = Immutable({
    rows:[],
    row:{},
    curTabKey:"home",
    detail:{
        title:"title",
        items:[]
    },
    TYPES:[
        "搭配","轮播","背景图"
    ],
    fetchingDetail:true,
    pageInfo:undefined,
    activeToolIndex:0,
    selectBtn:undefined
});
function delModule(state,index){
    let items = state.detail.items;
    items = items.flatMap((value,i)=>{
        if(i == index){
            return [];
        }else{
            return value;
        }
    });
    return state.setIn(['detail','items'],items);
}
import diyState from './json/diy';
import swiperState from './json/swiper';
import bottomState from './json/bottom';
function getState(type){
    if(type == 0) return diyState;
    if(type == 1) return swiperState;
    if(type == 2) return bottomState;
}
export default function app(state = initialState, action = {}) {
    switch (action.type) {
        case types.DEL_MODULE:
            return delModule(state,action.index);
        case types.ADD_MODULE:
            let data = {
                    type:parseInt(action.index),
                    status:0,
                    order:0,
                    state:getState(action.index)
                };
            let items = state.detail.items.asMutable();
            items.push(data);
            return state.setIn(['detail','items'],items);
        case types.CHANGE_TAB_KEY:
            return state.set("curTabKey",action.key)
        case types.SET_ACTIVE_TOOL_INDEX:
            if(action.data){
                if(action.data.selectBtnIndex){
                    return state.merge({
                        activeToolIndex:action.index,
                        selectBtnIndex:action.data.selectBtnIndex,
                    });
                }else{
                    return state.merge({
                        activeToolIndex:action.index,
                        selectSwiperIndex:action.data.selectSwiperIndex,
                    });
                }
            }else{
                return state.set("activeToolIndex",action.index);
            }

        case types.FETCHING_DETAIL:
            return state.set("fetchingDetail",action.isDo);
        case types.SET_ROWS:
            return state.set("rows",action.rows);
        case types.SET_DETAIL:
            return state.set("detail",action.row);
        case types.SET_ROW:
            return state.merge({
                row:action.row
            });
        default:
            return state;
    }
}
